Passed
Push — master ( 63524c...dd4398 )
by Night
01:10
created

stringFuncs.containsCI   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/** global: UB */
2
3
var stringFuncs = {
4
	
5
	
6
	beginsWith: function(prefix, length = 0){
7
		return this.smartStartsWith(prefix, length, true);
8
	},
9
	beginsWithCI: function(prefix, length = 0){
10
		return this.smartStartsWith(prefix, length, false);
11
	},
12
	startsWith: function(prefix, length = 0){
13
		return this.smartStartsWith(prefix, length, true);
14
	},
15
	startsWithCI: function(prefix, length = 0){
16
		return this.smartStartsWith(prefix, length, false);
17
	},
18
	smartBeginsWith: function(prefix, length = 0, caseSensitive = true, trimAndCheck = false){
19
		return this.smartStartsWith(prefix, length, caseSensitive, trimAndCheck);
20
	},
21
	smartStartsWith: function(prefix, length = 0, caseSensitive = true, trimAndCheck = false){
22
		var text = this;
23
	
24
		// trim and check if needed
25
		if (trimAndCheck) {
26
			text = text.replace(UB.regex.Trim, '');
27
		}
28
		
29
		// "starts with any"
30
		if (prefix.constructor == Array){
31
			
32
			// per search term
33
			for (var s = 0, sl = prefix.length;s<sl;s++){
34
				var pre = prefix[s];
35
				
36
				// ends with one
37
				if (text.smartStartsWith(pre, length, caseSensitive)) {
38
					return true;
39
				}
40
			}
41
			
42
			// doesnt end with any given
43
			return false;
44
		}
45
46
47
		// JS startsWith() API support
48
		if (length > 0 && prefix.length > length){
49
			prefix = prefix.substr(0, length);
50
		}
51
52
		// "starts with"
53
		var l1 = text.length;
54
		var l2 = prefix.length;
55
		
56
		// check quickly if same/less length
57
		if (l1 < l2) {
58
			return false;
59
		}
60
		if (l1 == l2) {
61
			if (caseSensitive) {
62
				return text == prefix;
63
			}
64
			return text.toLowerCase() == prefix.toLowerCase();
65
		}
66
		
67
		// check using substring if main string longer
68
		if (caseSensitive) {
69
			//return text.substr(0, l2) == prefix;
70
			return text.lastIndexOf(prefix, 0) == 0;
1 ignored issue
show
Best Practice introduced by
Comparing text.lastIndexOf(prefix, 0) to 0 using the == operator is not safe. Consider using === instead.
Loading history...
71
		}
72
		return text.substr(0, l2).toLowerCase() == prefix.toLowerCase();
73
	},
74
	endsWith: function(postfix, length = 0){
75
		return this.smartEndsWith(postfix, length, true);
76
	},
77
	endsWithCI: function(postfix, length = 0){
78
		return this.smartEndsWith(postfix, length, false);
79
	},
80
	smartEndsWith: function(postfix, length = 0, caseSensitive = true, trimAndCheck = false){
81
		var text = this;
82
	
83
84
		// trim and check if needed
85
		if (trimAndCheck) {
86
			text = text.replace(UB.regex.Trim, '');
87
		}
88
		
89
		// "ends with any"
90
		if (postfix.constructor == Array){
91
			
92
			// per search term
93
			for (var s = 0, sl = postfix.length;s<sl;s++){
94
				var post = postfix[s];
95
				
96
				// ends with one
97
				if (text.smartEndsWith(post, length, caseSensitive)) {
98
					return true;
99
				}
100
			}
101
			
102
			// doesnt end with any given
103
			return false;
104
		}
105
106
		// JS endsWith() API support
107
		if (length > 0 && postfix.length > length){
108
			postfix = postfix.substr(0, length);
109
		}
110
111
		// "ends with"
112
		var l1 = text.length;
113
		var l2 = postfix.length;
114
		
115
		// check quickly if same/less length
116
		if (l1 < l2) {
117
			return false;
118
		}
119
		if (l1 == l2) {
120
			if (caseSensitive) {
121
				return text == postfix;
122
			}
123
			return text.toLowerCase() == postfix.toLowerCase();
124
		}
125
		
126
		// check using substring if main string longer
127
		if (caseSensitive) {
128
			//return text.substr(l1 - l2, l2) == postfix;
129
			var index = l1 - l2;
130
			return text.indexOf(postfix, index) == index;
131
		}
132
		return text.substr(l1 - l2, l2).toLowerCase() == postfix.toLowerCase();
133
	},
134
	contains: function(substring){
135
		return this.smartContains(substring, true);
136
	},
137
	containsCI: function(substring){
138
		return this.smartContains(substring, false);
139
	},
140
	smartContains: function(substring, caseSensitive = true, substringIsLower = false, wholeWords = false){
141
		var text = this;
142
		
143
		// "contains any"
144
		if (substring.constructor == Array){
145
			
146
			// per search term
147
			for (var s = 0, sl = substring.length;s<sl;s++){
148
				var sub = substring[s];
149
				
150
				// ends with one
151
				if (text.smartContains(sub, caseSensitive, substringIsLower, wholeWords)) {
152
					return true;
153
				}
154
			}
155
			
156
			// doesnt end with any given
157
			return false;
158
		}
159
160
		// "contains"
161
		if (wholeWords) {
162
			return text.match(UB.regex.New(substring, wholeWords, caseSensitive, true)).length > 0;
163
		}
164
		if (caseSensitive) {
165
			return text.indexOf(substring) != -1;
166
		}
167
		/*
168
		text = text.toLowerCase();
169
		substring = substring.toLowerCase();
170
		
171
		return text.indexOf(substring) != -1;*/
172
		
173
		
174
		// quick checks
175
		if (text.length < substring.length) {
176
			return false;
177
		}
178
		
179
		
180
		// very fast CI comparison
181
		
182
		// init casing tables
183
		if (UB.UTF_lowerToUpper == null){
1 ignored issue
show
Best Practice introduced by
Comparing UB.UTF_lowerToUpper to null using the == operator is not safe. Consider using === instead.
Loading history...
184
			UB.initCasing();
185
		}
186
		
187
		//temps
188
		var sl = substring.length;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable sl already seems to be declared on line 147. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
189
		var ml = (text.length - sl);
190
		
191
		// per main char
192
		for (var m = 0;m <= ml;m++){
193
			
194
			
195
			// per substring char
196
			var match = true;
197
			for (var s = 0;s<sl;s++){
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable s already seems to be declared on line 147. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
198
				
199
				var c1 = text.charCodeAt(m + s);
200
				var c2 = substring.charCodeAt(s);
201
				
202
				// CI
203
				if (c1 <= UB.UTF_casingTablesMax){ /// CI
204
					c1 = UB.UTF_upperToLower[c1];
205
				}
206
				if (!substringIsLower){
207
					if (c2 <= UB.UTF_casingTablesMax){ /// CI
208
						c2 = UB.UTF_upperToLower[c2];
209
					}
210
				}
211
				
212
				if (c1 != c2) {
213
					match = false;
214
					break;
215
				}
216
				
217
			}
218
			
219
			if (match){
220
				return true;
221
			}
222
			
223
		}
224
		
225
		return false;
226
		
227
		/*return text.toLowerCase().indexOf(substring.toLowerCase()) != -1;*/
228
	},
229
	beginningOf: function(text, caseSensitive = true){
230
		var prefix = this;
231
		return text.startsWith(prefix, caseSensitive);
232
	},
233
	endingOf: function(text, caseSensitive = true){
234
		var postfix = this;
235
		return text.endsWith(postfix, caseSensitive);
236
	},
237
	containedIn: function(text, caseSensitive = true, substringIsLower = false){
238
		var substring = this;
239
		return text.contains(substring, caseSensitive, substringIsLower);
240
	},
241
	
242
	// contains
243
	containsAll: function(checkPerWord, searchFor, caseSensitive = true){
244
		var str = this;
245
		
246
		// split haystack into words
247
		var words = checkPerWord?str.split(" "):[str];
248
		
249
		// compare each word of needle seperately
250
		var matched = 0;
251
		for (var w = 0, wl = words.length;w<wl;w++){
252
			
253
			var word = words[w];
254
			
255
			// return true if any haystack S.Contains the needle
256
			for (var s = 0, sl = searchFor.length; s < sl; s++) {
257
				var sWord = searchFor[s];
258
				if (word.smartContains(sWord, caseSensitive)) {
259
					matched++;
260
				}
261
			}
262
		}
263
		
264
		return matched == searchFor.length;
265
	},
266
	containsNumber: function(){
267
		var str = this;
268
		for (var c = 0, cl = str.length;c<cl;c++){
269
			var char = str.charAt(c);
270
			if (char.isNumber()) {
271
				return true;
272
			}
273
		}
274
		return false;
275
	},
276
	containsAlphaNumeric: function(){
277
		var str = this;
278
		for (var c = 0, cl = str.length;c<cl;c++){
279
			var char = str.charAt(c);
280
			if (char.isAlphaNumeric()) {
281
				return true;
282
			}
283
		}
284
		return false;
285
	},
286
	
287
	
288
	endsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
289
		var str = this;
290
		
291
		// trim and check if needed
292
		if (trimAndCheck) {
293
			str = str.replace(UB.regex.Trim, '');
294
		}
295
		
296
		// per search term
297
		var indices = [];
0 ignored issues
show
Unused Code introduced by
The variable indices seems to be never used. Consider removing it.
Loading history...
298
		for (var s = 0, sl = searchFor.length;s<sl;s++){
299
			var sWord = searchFor[s];
300
			
301
			// ends with one
302
			if (str.endsWith(sWord, caseSensitive)) {
303
				return s;
304
			}
305
		}
306
		
307
		// doesnt end with any given
308
		return -1;
309
	},
310
	/** checks if the string begins with any of the given terms, and returns the char index of the first found term */
311
	beginsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
312
		return this.startsWithAny_Index(searchFor, caseSensitive, trimAndCheck);
313
	},
314
	/** checks if the string begins with any of the given terms, and returns the char index of the first found term */
315
	startsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
316
		var str = this;
317
		
318
		// trim and check if needed
319
		if (trimAndCheck) {
320
			str = str.replace(UB.regex.Trim, '');
321
		}
322
		
323
		// per search term
324
		var indices = [];
0 ignored issues
show
Unused Code introduced by
The variable indices seems to be never used. Consider removing it.
Loading history...
325
		for (var s = 0, sl = searchFor.length;s<sl;s++){
326
			var sWord = searchFor[s];
327
			
328
			// ends with one
329
			if (str.startsWith(sWord, caseSensitive)) {
330
				return s;
331
			}
332
		}
333
		
334
		// doesnt end with any given
335
		return -1;
336
	},
337
	
338
	/** checks if the string contains any of the given terms
339
	 * @param	searchFor		an array of search terms
340
	 */
341
	containsAny_Index: function(searchFor, caseSensitive = true){
342
		var str = this;
343
		
344
		// return index if found
345
		for (var s = 0, sl = searchFor.length; s < sl; s++) {
346
			sWord = searchFor[s];
0 ignored issues
show
Bug introduced by
The variable sWord seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sWord.
Loading history...
347
			var i = S.IndexOf(str, sWord, caseSensitive);
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
348
			if (i > -1){
349
				return i;
350
			}
351
		}
352
		
353
		return -1;
354
	},
355
	
356
	none:null
357
};
358
359
// register funcs
360
UB.registerFuncs(String.prototype, stringFuncs);